﻿package com.badibadi.logic;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.badibadi.activity.BaseActivity;
import com.badibadi.infos.ImageAttribute;
import com.badibadi.mytools.Utils;
import com.badibadi.uniclubber.R;
//import com.lidroid.xutils.util.LogUtils;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

/**
 * 选择相册的activity
 * 
 * @author Administrator
 * 
 */
public class ImgFileListActivity extends BaseActivity implements
		OnItemClickListener {
	private ListView listView;
	private Util util;
	private ImgFileListAdapter listAdapter;
	private List<FileTraversal> locallist;
	private String panduan = null, club = null, no_dou, photoid = null,
			activity_id = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		goToTheTitle();
		setContentView(R.layout.img_filelist);
		panduan = getIntent().getStringExtra("panduan");
		club = getIntent().getStringExtra("club");
		no_dou = getIntent().getStringExtra("no_dou");
		photoid = getIntent().getStringExtra("photoid");
		activity_id = getIntent().getStringExtra("activity_id");
		this.findViewById(R.id.return_btn).setOnClickListener(
				new View.OnClickListener() {

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						finish();
					}
				});
		listView = (ListView) findViewById(R.id.listView1);
		util = new Util(this);
		locallist = util.LocalImgFileList();
		List<HashMap<String, String>> listdata = new ArrayList<HashMap<String, String>>();
		// Bitmap bitmap[] = null;
		if (locallist != null) {
			// bitmap = new Bitmap[locallist.size()];
			for (int i = 0; i < locallist.size(); i++) {
				HashMap<String, String> map = new HashMap<String, String>();
				map.put("filecount", locallist.get(i).filecontent.size() + "张");
				//设置相册图片最新的为首页
				map.put("imgpath",
						locallist.get(i).filecontent.get(0) == null ? null
								: (locallist.get(i).filecontent.get(locallist.get(i).filecontent.size()-1)));
				map.put("filename", locallist.get(i).filename);
				listdata.add(map);

			}
		}
		listAdapter = new ImgFileListAdapter(this, listdata);
		listView.setAdapter(listAdapter);
		listView.setOnItemClickListener(this);
//		LogUtils.d("1="
//				+ com.badibadi.mytools.Dialog.getSystemLanguageType(this)
//				+ "2="
//				+ com.badibadi.mytools.Dialog.getSystemLanguageTypegrzx(this)
//				+ "3="
//				+ com.badibadi.mytools.Dialog.getSystemLanguageTypegy(this));
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
//		   
		if (requestCode == 5 && resultCode == 5) {
			ArrayList<String> filelist = data.getStringArrayListExtra("files");
			ImageAttribute attribute = (ImageAttribute) data
					.getSerializableExtra("img_info");
			Intent intent = new Intent();
			intent.putExtra("files", filelist);
			if (attribute != null) {
				intent.putExtra("img_info", attribute);
			}
			setResult(5, intent);
			finish();
		}
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		Intent intent = new Intent(this, ImgsActivity.class);
		Bundle bundle = new Bundle();
		bundle.putParcelable("data", locallist.get(arg2));
		bundle.putString("panduan", panduan);
		if (club != null) {
			bundle.putString("club", club);
		}
		if (no_dou != null) {
			bundle.putString("no_dou", no_dou);
		}
		if (photoid != null) {
			intent.putExtra("photoid", photoid);
		}
		if(activity_id!=null){
			intent.putExtra("activity_id", activity_id);
		}
		intent.putExtras(bundle);
		startActivityForResult(intent, 5);
		// if (panduan.equals("set")) {
		// this.finish();
		// }
	}
}
